package com.xboss.tv.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.hejunlin.tvsample.widget.MetroViewBorderImpl;
import com.xboss.tv.R;
import com.xboss.tv.bean.Contacts;

/**
 * Created by Administrator on 2017/7/17.
 */

public class UrlDataActivity extends AppCompatActivity implements View.OnClickListener {

    private WebView mWebView;
    private TextView mTextHome;
    private TextView mTextBack;
    private int flag = 0;

    private ConstraintLayout mLinear;
    private MetroViewBorderImpl mMetroViewBorderImpl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_url);

        Contacts.addActivity(UrlDataActivity.this);

        String url = getIntent().getStringExtra("url");
        mTextHome = (TextView) findViewById(R.id.detail_image_home);
        mTextBack = (TextView) findViewById(R.id.detail_image_back);
        mLinear = (ConstraintLayout) findViewById(R.id.detail_linear);

        mTextHome.setOnClickListener(this);
        mTextBack.setOnClickListener(this);

        mWebView = (WebView) findViewById(R.id.url_webview);

        mWebView.getSettings().setJavaScriptEnabled(false);

        hideKeyboard(mWebView);

        //使用自定义的WebViewClient
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);

                //如果不需要其他对点击链接事件的处理返回true，否则返回false
                return true;

            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);

                Log.d("TAG", "description = " + description + ", errorCode = " + errorCode);
            }

        });

        //加载需要显示的网页
        mWebView.loadUrl(url);
    }

    //隐藏虚拟键盘
    public void hideKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
            imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.detail_image_home) {//返回主页
            Contacts.removeActivitys();
            Contacts.isLoad = false;

        } else if (id == R.id.detail_image_back) {//退出详情页面

            finish();
        }
    }

    public void startScaleAnim(View view) {
        view.setFocusable(true);
        view.setBackgroundResource(R.drawable.border_color);

        ScaleAnimation animation = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(200);//设置动画持续时间

        animation.setFillAfter(true);
        view.setAnimation(animation);
        animation.startNow();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        Log.d("TAG", "按下了enter键");


        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {

        if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {

            return false;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Contacts.isLoad = true;
    }
}
